﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.IO;
using System;
using System.Collections.Generic;

namespace Blueprints.Editing.Actions
{
    // this also allows creating a property value, replacing an existing property value.
    internal sealed class CreatePropertyNodeAction : IUndoableAction
    {
        public CreatePropertyNodeAction(EditNode node, string name, string value)
        {
            Node = node;
            Name = name;
            Value = value;
            int nodeIndex = node.EditContext.Nodes.IndexOfPropertyNode(node, name);
            if (nodeIndex < 0)
            {
                BeforeNodes = new List<EditNode>();
            }
            else
            {
                BeforeNodes = node.EditContext.Nodes.GetSubTree(nodeIndex);
            }

            AfterNodes = new List<EditNode>();
            AfterNodes.Add(new EditNode(node.EditContext, new StartNode(name, null, null, null)));
            if (!string.IsNullOrEmpty(value))
            {
                AfterNodes.Add(new EditNode(node.EditContext, new ContentNode(value)));
            }

            AfterNodes.Add(new EditNode(node.EditContext, new EndNode()));
            
            EditProperty property;
            if (node.Properties.TryGetValue(name, out property))
            {
                Subject = AfterNodes[0];
            }
            else
            {
                Subject = node;
            }
        }

        public CreatePropertyNodeAction(EditNode node, string name)
            : this(node, name, null)
        {
        }

        public EditNode Node
        {
            get;
            private set;
        }

        public EditNode Subject
        {
            get;
            private set;
        }

        public string Name
        {
            get;
            private set;
        }

        public string Value
        {
            get;
            private set;
        }

        public List<EditNode> BeforeNodes
        {
            get;
            private set;
        }

        public List<EditNode> AfterNodes
        {
            get;
            private set;
        }
        
        public EditNode Do()
        {
            int startNodeIndex = Node.EditContext.Nodes.IndexOf(Node);
            if (startNodeIndex < 0)
            {
                throw new InvalidOperationException();
            }

            int nodeIndex = Node.EditContext.Nodes.IndexOfPropertyNode(startNodeIndex, Name);
            if (nodeIndex >= 0)
            {
                Node.EditContext.Nodes.RemoveSubTree(nodeIndex);
            }
            else
            {
                nodeIndex = Node.EditContext.Nodes.IndexOfEndNode(startNodeIndex);
            }

            Node.EditContext.Nodes.InsertRange(nodeIndex, AfterNodes);
            return Subject;
        }

        public EditNode Undo()
        {
            int startNodeIndex = Node.EditContext.Nodes.IndexOf(Node);
            if (startNodeIndex < 0)
            {
                throw new InvalidOperationException();
            }

            int nodeIndex = Node.EditContext.Nodes.IndexOfPropertyNode(startNodeIndex, Name);
            if (nodeIndex >= 0)
            {
                Node.EditContext.Nodes.RemoveSubTree(nodeIndex);
            }
            else
            {
                nodeIndex = Node.EditContext.Nodes.IndexOfEndNode(startNodeIndex);
            }

            Node.EditContext.Nodes.InsertRange(nodeIndex, BeforeNodes);
            return Node;
        }
    }
}
